
//
//  UserViewController.m
//  News
//
//  Created by xalo on 16/3/7.
//  Copyright © 2016年 News. All rights reserved.
//

#import "UserViewController.h"
#import "UserViewCell1.h"
#import "UserViewCell2.h"
#import "UserViewCell3.h"
#import "CollectionController.h"

#import <SDWebImageManager.h>

#import "AboutViewController.h"
#import "UIViewController+Notice.h"

#import "UIImageView+WebCache.h"
#import "LoginHelp.h"
#import "UMSocial.h"
#import "LoginViewController.h"

@interface UserViewController ()

@property (strong, nonatomic) IBOutlet UIView *headView;
@property (nonatomic,assign) NSInteger bytes;

- (IBAction)loginAction:(id)sender;

- (IBAction)switchAction:(UISwitch *)sender;

@property (nonatomic,assign) BOOL isNight;
@property (nonatomic,assign) BOOL isWifi;

@property (nonatomic,strong) UIView *headerView;

@property (strong, nonatomic) IBOutlet UIImageView *useImage;

@property (strong, nonatomic) IBOutlet UILabel *useLa;

@property (nonatomic,strong) LoginViewController *loginVC;

@end

@implementation UserViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isNight = NO;
    self.isWifi = NO;
    
    self.tableView.rowHeight = 44;
    
    self.headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/4);
    
   
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundView = ({
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        view;
    
        });
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    //
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    [self.tableView reloadData];
    
    
}





#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UserViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
//    
    NSInteger index = indexPath.section;
    
    switch (index) {
        case 0:{
             UserViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
            
            cell.titleLabel.text = @"收藏";
            cell.pictureView.image = [UIImage imageNamed:@"Favorites"];
            [cell.clickButton addTarget:self action:@selector(collectAction) forControlEvents:(UIControlEventTouchUpInside)];
            
            
            [self changeUserCellBackground:cell];
            
            
            return cell;
            
        }
            break;
        case 1:{
            UserViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
            
            cell.titleLabel.text = @"夜间模式";
            cell.pictureView.image = [UIImage imageNamed:@"night"];
            
            cell.switchButton.tag = 101;
            [cell.switchButton setOn:self.isNight];
            [self changeUserCellBackground:cell];
             return cell;
            
            }
            
            break;
        case 2:{
        
            UserViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
            
            cell.titleLabel.text = @"仅Wi-Fi下载图片";
            cell.pictureView.image = [UIImage imageNamed:@"wifi"];
            cell.switchButton.tag = 102;
            [cell.switchButton setOn:self.isWifi];
            [self changeUserCellBackground:cell];
             return cell;
        }
            break;
        case 3:{
        
          UserViewCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
            
          cell.titleLabel.text = @"清除缓存";
            cell.pictureView.image = [UIImage imageNamed:@"clean"];
            [cell.cleanButton addTarget:self action:@selector(cleanAction:) forControlEvents:(UIControlEventTouchUpInside)];
            self.bytes = [SDImageCache sharedImageCache].getSize;
            NSString *caches = [NSString stringWithFormat:@"%.1fM",self.bytes/1000.0/1000.0];
           cell.cachesLabel.text = caches;
            [self changeUserCellBackground:cell];
             return cell;
            
        }
            
            break;
        case 4:{
            UserViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
            
            cell.titleLabel.text = @"关于";
            cell.pictureView.image = [UIImage imageNamed:@"logo"];
            [cell.clickButton addTarget:self action:@selector(aboutAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self changeUserCellBackground:cell];
             return cell;
        }
            
//            break;
//        case 5:{
//            UserViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
//            
//            cell.titleLabel.text = @"退出登陆";
//            cell.titleLabel.textColor = [UIColor redColor];
//            cell.pictureView.image = [UIImage imageNamed:@"Pressed"];
//            [cell.clickButton addTarget:self action:@selector(exitAction) forControlEvents:(UIControlEventTouchUpInside)];
//            [self changeUserCellBackground:cell];
//            return cell;
//        
//        }

        default:
            break;
    }
    
    return nil;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    self.headerView = [[UIView alloc] init];
    
    if (self.isNight) {
        
        self.headerView.backgroundColor = [UIColor lightGrayColor];
    }else{
    
    self.headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    
    return self.headerView;
}

//收藏//
-(void)collectAction
{
    CollectionController *collectionVC = [[CollectionController alloc] init];
    
    [self.navigationController pushViewController:collectionVC animated:YES];
    
}
//清楚缓存
-(void)cleanAction:(UIButton *)button
{

    [[SDImageCache sharedImageCache] clearDisk];
    
    [self.tableView reloadData];

}

//关于
-(void)aboutAction
{
     AboutViewController *aboutVC = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"about"];
    [self.navigationController pushViewController:aboutVC animated:YES];
    
}
//退出应用aasdadadadadasdasdsa
-(void)exitAction
{
    BOOL result = [LoginHelp shareLoginHelp].islogin;
    
    if (result) {
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认退出当前账号" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self exitApplication];
            
        }];
        
        [alertVC addAction:alert];
        [alertVC addAction:alert2];
        
        [self presentViewController:alertVC animated:YES completion:nil];
        
        [LoginHelp shareLoginHelp].islogin = NO;
        
    }
    else {
     
        [self exitLogin];
        
    }
    
}

/*//进入登陆界面
- (IBAction)loginAction:(id)sender {
    
    
    if ([LoginHelp shareLoginHelp].islogin) {
        
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否切换账号" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *left = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        
        
        UIAlertAction *right = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self agreeAction];
            
        }];
        
        
        [alter addAction:left];
        [alter addAction:right];
        [self presentViewController:alter animated:YES completion:nil];
        
//                        [LoginHelp shareLoginHelp].islogin = YES;
        
    }
    else {
        
        
        self.tabBarController.tabBar.hidden = YES;
        
        [self disclaimerView];
        
        
        
        
//
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
//
//        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//
//            //          获取微博用户名、uid、token等
//
//            if (response.responseCode == UMSResponseCodeSuccess) {
//
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
//
//                //            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//
//                [self.useImage  sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
//                [self.useImage layoutIfNeeded];
//                self.useImage.layer.cornerRadius = CGRectGetHeight(self.useImage.frame)/2;
//                self.useImage.layer.masksToBounds =YES;
//
//
//                self.useLa.text = snsAccount.userName;
//
//            }
//        });
//        
//        
    }

    
}
*/


-(void)disclaimerView {

    self.loginVC = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"login"];
    self.loginVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
    [self presentViewController:self.loginVC animated:YES completion:nil];
    [self.loginVC.cancelAction addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.loginVC.agreeAction addTarget:self action:@selector(agreeAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    


}

-(void)cancelAction{
    
    self.tabBarController.tabBar.hidden = NO;

    [self.loginVC dismissViewControllerAnimated:YES completion:nil];
    
    

}
-(void)agreeAction{


    self.tabBarController.tabBar.hidden = NO;

    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            //            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            [self.useImage  sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
            [self.useImage layoutIfNeeded];
            self.useImage.layer.cornerRadius = CGRectGetHeight(self.useImage.frame)/2;
            self.useImage.layer.masksToBounds =YES;
            
            
            self.useLa.text = snsAccount.userName;
            
            [LoginHelp shareLoginHelp].islogin = YES;
        }
    });

    [self dismissViewControllerAnimated:YES completion:nil];

}








- (IBAction)switchAction:(UISwitch *)sender {
    
    if (sender.tag == 101) {
        
        self.isNight = !self.isNight;
        
        extern BOOL isNightMode;
         isNightMode = self.isNight;
        
            [[NSNotificationCenter defaultCenter] postNotificationName:Knight object:self];
        
            [self changeBackgroung];
        
    }else if (sender.tag == 102){
    
            self.isWifi = !self.isWifi;
        extern BOOL isWiFiMode;
        isWiFiMode = self.isWifi;
        
            NSLog(@"开启wifimoshi");
            [[NSNotificationCenter defaultCenter] postNotificationName:Kwifi object:self];
       
        }
    
}

//通知方法
-(void)changeBackgroung
{
    
    if (self.isNight) {
        
        
        [[UINavigationBar appearance] setBarTintColor:[UIColor darkGrayColor]];
        [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
        [[UITableViewCell appearance] setBackgroundColor:[UIColor darkGrayColor]];
        [[UITableView appearance] setBackgroundColor:[UIColor lightGrayColor]];
        self.tabBarController.tabBar.opaque = YES;
        self.tabBarController.tabBar.barTintColor = [UIColor darkGrayColor];
        
        self.headView.alpha = 0.5;
        
        self.tableView.backgroundView = ({
            UIView *view = [[UIView alloc]init];
            view.backgroundColor = [UIColor lightGrayColor];
            view;
            
        });
        
        
    }else{
        
        
        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
        [[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
        [[UITableViewCell appearance] setBackgroundColor:[UIColor whiteColor]];
        [[UITableView appearance] setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        self.tabBarController.tabBar.opaque = YES;
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];

        self.headView.alpha = 1.0;
        self.tableView.backgroundView = ({
            UIView *view = [[UIView alloc]init];
            view.backgroundColor = [UIColor groupTableViewBackgroundColor];
            view;
            
        });

    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        
        [self.tableView reloadData];
        
    });
    
    
    
}



-(void)changeUserCellBackground:(UITableViewCell *)cell
{
    if (self.isNight) {
        
        cell.backgroundColor = [UIColor darkGrayColor];
        
    }else{
    
        cell.backgroundColor = [UIColor whiteColor];
    
    }


}





- (void)exitApplication {
    
    
    
        self.useImage.image = [UIImage imageNamed:@"iconfont-iconfontmyfill"];
        self.useLa.text = @"使用第三方QQ登陆账号";
    


}

- (void)exitLogin {


    [self showAlertView];
}


//没网是弹出警示框
-(void)showAlertView
{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:nil];
    
    [self performSelector:@selector(diss) withObject:nil afterDelay:1.0];
    
}




- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    if ([animationID compare:@"exitApplication"] == 0) {
        
        exit(0);
        
    }
    
}

-(void)nextLogn {
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            //            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            [self.useImage  sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
            [self.useImage layoutIfNeeded];
            self.useImage.layer.cornerRadius = CGRectGetHeight(self.useImage.frame)/2;
            self.useImage.layer.masksToBounds =YES;
            
            
            self.useLa.text = snsAccount.userName;
            
            
            
        }
    });

        
}

//提示框消失时间
-(void)diss {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


@end
